/*
 * File Name：ScanImageBottom.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： ScanImageBottom.java
 * Modify By：res-jxliu
 * Modify Date：2015年4月6日
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;

import com.ciwong.libs.utils.DeviceUtils;
import com.ciwong.mobilelib.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.LinearLayout.LayoutParams;

/**
 * 
 * 
 * @author res-jxliu
 * @version ciwong v.1.0 2015年4月6日
 * @since ciwong v.1.0
 */
public class ScanImageBottom extends LinearLayout
{
    /**
     * 菜单速度
     */
    private static final int SHOW_SPEED = 200;

    private Scroller mScroller;

    private int screenHeight;

    private int height;

    private int bottomHeight;

    private boolean show = true;

    public boolean isShow()
    {
        return show;
    }

    public ScanImageBottom(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    public ScanImageBottom(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public ScanImageBottom(Context context)
    {
        super(context);
        init();
    }

    private void init()
    {
        mScroller = new Scroller(getContext());
        View.inflate(getContext(), R.layout.scan_image_bottom, this);
        measure(MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT,
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY));
        bottomHeight = getBottom();
        height = getMeasuredHeight() - 1;
        screenHeight = DeviceUtils.getScreenHeight();
    }

    /**
     * 显示
     * 
     */
    public void show()
    {
        if (bottomHeight == 0)
        {
            bottomHeight = getBottom();
        }
        mScroller.startScroll(0, -height, 0, height, SHOW_SPEED);
        invalidate();
        show = true;
    }

    /**
     * 隐藏
     * 
     */
    public void hide()
    {
        if (bottomHeight == 0)
        {
            bottomHeight = getBottom();
        }
        mScroller.startScroll(0, 0, 0, -height, SHOW_SPEED);
        invalidate();
        show = false;
    }

    public void initHide()
    {
        setMarginBottom(-height);
        show = false;
    }

    @Override
    public void computeScroll()
    {
        if (mScroller.computeScrollOffset())
        {
            setMarginBottom(mScroller.getCurrY());
            invalidate();
        }
    }

    private void setMarginBottom(int marginBottom)
    {
        android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) getLayoutParams();
        params.bottomMargin = marginBottom;
        setLayoutParams(params);
    }
}
